package webClient.UserInterfaceApplet;


import javax.swing.*;

import communication.*;

import webClient.ChatEngine.RequestHandler;
import webClient.UserInterfaceClasses.*;

import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;


public class clientapp extends JApplet implements ActionListener,Runnable
{
	JPanel panel = new JPanel();
	JPanel panel2 = new JPanel();
	List list = new List(10);
	JTextArea textfield = new JTextArea();
	JTextField sendField = new JTextField();
	JButton button = new JButton();
	JButton invite = new JButton();
	JButton button2 = new JButton();
	Font ver = new Font("Courier",1,12);
	Font ver2 = new Font("Courier",1,14);
	JScrollPane messageScrollPane = new javax.swing.JScrollPane();
	
	JButton exit = new JButton();

	private WebChat chat = null; 
	
	// All Strings
	
	String name2;
	String texta;
	String send;
	String receive;
	
	// Threads
	Thread t1 = new Thread(this);
	
	public void init()
	{
		getContentPane().add(panel);
		panel.setLayout(null);
		panel.setBackground(new Color(0));	

		
		textfield.setBounds(5,5,420,250);
		textfield.setFont(ver);
		textfield.append("Chat Applet\n\n");
		textfield.setWrapStyleWord(true);
		textfield.setLineWrap(true);
		textfield.setColumns(20);
		textfield.setRows(5);
		textfield.setAutoscrolls(true);
		textfield.setEditable(false);
		messageScrollPane = new JScrollPane(textfield);
		messageScrollPane.setBounds(5,5,420,250);
		
		/*JPanel messagepanel = new JPanel();
		javax.swing.GroupLayout detailsPanelLayout = new javax.swing.GroupLayout(messagepanel);
        messagepanel.setLayout(detailsPanelLayout);
        detailsPanelLayout.setHorizontalGroup(
            detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(messageScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)
                .addContainerGap())
        );
        detailsPanelLayout.setVerticalGroup(
            detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailsPanelLayout.createSequentialGroup()
                .addComponent(messageScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                .addContainerGap())
        );
		panel.add(messagepanel);*/
		
		panel.add(messageScrollPane);
		
		panel.add(sendField);
		sendField.setBounds(5,265,420,25);
		sendField.setFont(ver);
		sendField.setText("");
		
		panel.add(button);
		button.setBounds(5,300,420,25);
		button.setText("Send");
		button.setFont(ver);
		button.addActionListener(this);
		
		panel.add(invite);
		invite.setBounds(5,335,420,25);
		invite.setText("Invite buddy");
		invite.setFont(ver);
		invite.addActionListener(this);
		
		/*panel.add(exit);
		exit.setBounds(385,335,70,25);
		exit.setFont(ver);
		exit.setText("exit");
		exit.addActionListener(this);*/
		
		t1.start();		
		
	}
	
	
	
		
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == button)
		{	
			texta = sendField.getText();
			sendField.setText("");
			chat.sendMessage(texta);
		}else if(e.getSource() == invite){
			String buddyname = JOptionPane.showInputDialog("Insert Buddy's name:");
			
			if(buddyname != null){
				chat.inviteBuddy(buddyname);
			}
		}

		
	}
	
	public void printMessage(String user, String message){
		
		textfield.append(user + " >> " + message + "\n");
		textfield.setCaretPosition(textfield.getDocument().getLength());
		
	}
	
	
	public void run()
	{
		
		
		
	}
	
	public void setChat(WebChat chat){
		
		this.chat = chat;
		
	}



}